import query from '@dfeidao/atom-nodejs/db/query';
import format from '@dfeidao/atom-nodejs/dt/format';
import { systemid } from '../atom/config';

import log from '@dfeidao/atom-nodejs/logger/log';
import { IncomingHttpHeaders } from 'http';

// interface Message {
// 	// cookie: {
// 	// 	uk: string;
// 	// 	[key: string]: string
// 	// };
// 	// urls: {
// 	// 	base: string;
// 	// 	origin: string;
// 	// 	url: string;
// 	// };
// 	// query: {};
// 	// params: {};
// 	// headers: {};
// 	// captcha: string;
// }
interface Message {
	filter: {
		product_up_status: "已上架"
	};
	asc: string[];
	desc: string[];
	page_size: number;
	page_no: number;
}
interface merchandise_shelves {
	companyid: string;
	warehouseid: string;
	commodityid: string;
	itemvalue: string;
	equip_no: string;
	location_no: string;
	Date: string;
	pick_time: number;
}

// 去重
function heavy(arr: any) {
	// 接收不重复的变量
	const data: string[] = [];
	const tempdata: string[] = [];
	// 接收大于5条的变量
	const dataArray: string[] = [];
	// 遍历参数
	for (let i = 0; i < arr.length; i++) {
		const w_id = arr[i].warehouseid;
		// 判断data里是否有重复的值
		if (!tempdata.includes(w_id)) {
			// 将没有重复的值添加到data变量里
			tempdata.push(w_id);
			data.push(arr[i]);
		} else {
			// 遍历data
			for (let j = 0; j < data.length; j++) {
				// 判断这个值是data里的那一个
				if (data[j].warehouseid === w_id) {
					// 判断data里是否添加过一次count如果没有就是1
					if (!data[String(w_id)]) {
						data[String(w_id)] = { no: arr[i], count: 2 };
					} else {
						data[String(w_id)] = { no: arr[i], count: (data[w_id].count + 1) };
					}
				}
			}
		}
	}
	console.log('=====================', data);
	// 遍历data集合
	for (let i = 0; i < data.length; i++) {
		// 判断data里的数据count是否大于5,如果大于5就添加到dataArray里
		if (data[data[i].warehouseid].count > 5) {
			dataArray.push(data[i]);
		}
	}
	// 返回
	return dataArray;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:ready-list/s001,action_id:' + action_id);
	const { exec, prepare } = query(action_id, session_id, systemid);
	prepare('merchandise_shelves', ['_id', 'companyid', 'commodityid', 'warehouseid', 'equip_no', 'location_no'], message.filter, message.page_size, message.page_no, message.asc, message.desc);
	// 获取全部已上架的商品
	const [data] = await exec();
	console.log(data.length);
	const param = {
		$or: data.map((item: merchandise_shelves) => {
			return {
				companyid: item.companyid,
				warehouseid: item.warehouseid,
				commodityid: item.commodityid
			};
		})
	};
	const { exec: e2, prepare: p2 } = query(action_id, session_id, systemid);
	p2('z001_time', ['_id', 'itemvalue', 'companyid', 'warehouseid', 'location_no', 'itemname'], param, message.page_size, message.page_no, message.asc, message.desc);
	const [itemvalue] = await e2();
	const z002data: merchandise_shelves[] = [];
	itemvalue.forEach((item: merchandise_shelves) => {
		if (Number(item.itemvalue) > 2) {
			z002data.push(item);
		}
	});
	// 合并
	const M2 = data.reduce((p, c: merchandise_shelves) => {

		const obj = JSON.stringify({
			companyid: c.companyid,
			warehouseid: c.warehouseid,
			location_no: c.location_no
		});
		p.set(obj, c.equip_no);
		return p;
	}, new Map<string, string>());
	const data2 = itemvalue.map((i: merchandise_shelves) => {
		const obj = JSON.stringify({
			companyid: i.companyid,
			warehouseid: i.warehouseid,
			location_no: i.location_no
		});
		return { ...i, equip_no: M2.get(obj) };
	});
	console.log('---------__________________', data2);

	const heavydata = heavy(data2);
	if (heavydata.length === 0) {
		return;
	} else {
		const e_no = heavydata.map((item: any) => {
			return item.equip_no;
		});
		const { exec: e3, prepare: p3 } = query(action_id, session_id, systemid);
		p3('pick_list', ['_id', 'pick_no', 'retailer_store_name', 'pick_num', 'pick_time'], { retailer_store_no: { $in: e_no } }, message.page_size, message.page_no, message.asc, message.desc);

		const [pl_data] = await e3<merchandise_shelves>();
		pl_data.map((item) => {
			return item.Date = format(item.pick_time, 'yyyy-mm-dd HH:MM:ss');
		});
		return pl_data;
		log('Service end path:ready-list/s001,action_id:' + action_id);
		return {
			data: "pl_data"
		};
	}
